package com.miuugo.common.tools;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;

public class HttpRequestUtils {

    private static final PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();

    private static final Integer DEFAULT_TIMEOUT = 5000;
    private static final Integer DEFAULT_RETRY = 0;


    static {
        pool.setMaxTotal(200);
        pool.setDefaultMaxPerRoute(20);
    }

    public static CloseableHttpClient getHttpClient(Integer timeout, Integer retry) {

        if (timeout == null) {
            timeout = DEFAULT_TIMEOUT;
        }

        if (retry == null) {
            retry = DEFAULT_RETRY;
        }


        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout).build();
        return HttpClients.custom().setDefaultRequestConfig(requestConfig)
                .setRetryHandler(new DefaultHttpRequestRetryHandler(retry, false))
                .setConnectionManager(pool).build();
    }

    public static String get(String url) throws IOException {
        return get(url, null);
    }

    public static String get(String url, List<Header> headers) throws IOException {
        return get(url, headers, null, null);
    }

    public static String get(String url, List<Header> headers, Integer timeout, Integer retry) throws IOException {
        CloseableHttpClient httpClient = getHttpClient(timeout, retry);
        HttpGet httpGet = new HttpGet(url);

        if (headers != null && headers.size() > 0) {
            httpGet.setHeaders((Header[]) headers.toArray());
        }

        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        String msg = EntityUtils.toString(httpEntity);
        if (httpResponse != null) {
            EntityUtils.consume(httpEntity);
            httpResponse.close();
        }
        return msg;
    }

    public static String post(String url, HttpEntity body) throws IOException {
        return post(url,body,null);
    }

    public static String post(String url, HttpEntity body, List<Header> headers) throws IOException {
        return post(url,body,headers,null,null);
    }

    public static String post(String url, HttpEntity body, List<Header> headers, Integer timeout, Integer retry) throws IOException {
        CloseableHttpClient httpClient = getHttpClient(timeout, retry);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(body);

        if (headers != null && headers.size() > 0) {
            httpPost.setHeaders((Header[]) headers.toArray());
        }

        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        String msg = EntityUtils.toString(httpEntity);
        if (httpResponse != null) {
            EntityUtils.consume(httpEntity);
            httpResponse.close();
        }
        return msg;

    }

    public static void main(String[] args) {
        try {
            System.out.println(get("https://baidu.com"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
